<?php

// if file was loaded direclty, redirect to home page
if (basename(__FILE__) == basename($_SERVER['PHP_SELF'])) header("Location: index.php");

extract($_POST);


if(!in_array($_SERVER['HTTP_HOST'], array('localhost', '127.0.0.1', 'l'))){
    require_once 'Mail.php'; 
}

// require_once 'Mail.php'; 


$error = '';
$displayForm = true;

$to = COMPANY_EMAIL;



// if form was submitted
if( isset(  $send ) )
{
	$like = true;
	
	$displayForm = false;
	
	$send_copy = ( isset($copy) && $copy == true ) ? ", $email" : '';
	
	$params['host'] 	= 'ssl://smtp.gmail.com';
	$params['port'] 	= 465;
	$params['auth'] 	= true;
	$params['username'] = 'mail@apps.ge';
	$params['password'] = 'outstanding';
	
	$headers['Content-Type']= "text/plain; charset=\"UTF-8\"";
	$headers['From'] 		= "=?utf-8?B?".base64_encode($name)."?=";
	$headers['To'] 			= $to.$send_copy;
	$headers['Reply-To'] 	= $email;
	// $headers['Cc'] 			= $email;
	$headers['Subject']     = $subject;
	
	// $mail =& Mail::factory('smtp', array ('host' => 'ssl://smtp.gmail.com', 'port' => 465, 'auth' => true, 'username' => 'mail@apps.ge', 'password' => 'outstanding'));
	$mail =& Mail::factory('smtp', $params);
	
	// $headers = array("Content-Type" => "text/plain; charset=\"UTF-8\"", "From" => "=?utf-8?B?".base64_encode($name)."?=" . " <{$email}>", "Reply-To" => $email, $send_copy, "Subject" => $subject);
	$body = "სახელი: ".$name."\nტელეფონი: ".$phone."\nელ-ფოსტა:".$email."\n".$message;
		
	/* if anything was left empty */
	if( empty($name) OR empty($phone) OR empty($email) OR empty($subject) OR empty($message) ) {
		$error = '<div class="error"><p>შეავსეთ მოცემული ველები.</p></div>';
		$displayForm = true;
	}
	
	/* if email wasnt valid */
	else if(!preg_match('/^[^0-9][a-zA-Z0-9-_]*([.][a-zA-Z0-9-_]+)*[@][a-zA-Z0-9-_]+([.][a-zA-Z0-9-_]+)*[.][a-zA-Z]{2,4}$/', $email)) {
		$error = '<div class="error"><p>მიუთითეთ სწორი ელ-ფოსტა.</p></div>';
		$displayForm = true;
	}
		
	
	else if (PEAR::isError($mail->send($to, $headers, $body))) {
   		//$error = $mail->getMessage();
   		$error = '<div class="error"><p>ელ-ფოსტის გაგზავნა შეუძლებელია (Server Error!). </p></div>';		
		$displayForm = true;
	}
	

}

